import re

date_reg_exp = re.compile('\d{4}[-/]\d{1,2}[-/]\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}.\d{1,3}')


# fuel_reg_exp = re.compile(r'(?<=DisplayV=).[0-9]*')


class ModelLogFilter(object):
    def __init__(self, file_path, file_type, filter_cfg):
        self._header_list = None
        self._file_path = file_path
        self._file_type = file_type
        self._filter_cfg = filter_cfg
        self._filter_reg_exp = {}

    def _log_filter_reg_exp(self):
        for filter_info in self._filter_cfg:
            if filter_info["匹配类型"] == "完全匹配":
                for key_string in filter_info["匹配规则"]:
                    reg_exp = '(?<=' + key_string + ').[0-9]*'
                    self._filter_reg_exp[key_string] = reg_exp
            if filter_info["匹配类型"] == "正则匹配":
                pass  # 暂时不处理

    def log_filter_text(self):
        file_read = open(self._file_path, "r", encoding='utf-8', errors='ignore')
        file_read_lines = file_read.readlines()
        self._log_filter_reg_exp()
        filter_result = []
        for line_info in file_read_lines:
            dict_info = {}
            find_result = False
            find_info = date_reg_exp.findall(line_info)
            if len(find_info) > 0:
                dict_info['日期'] = find_info[0]
            else:
                continue
            for key in self._filter_reg_exp:
                pattern = re.compile(self._filter_reg_exp[key])
                find_list = pattern.findall(line_info)
                if len(find_list) > 0:
                    find_result = True
                    dict_info[key] = find_list[0]
                else:
                    dict_info[key] = ''
            if find_result:
                filter_result.append(dict_info)
        return filter_result

    def log_filter_header(self):
        self._header_list = ['日期']
        for filter_info in self._filter_cfg:
            if filter_info["匹配类型"] == "完全匹配":
                for key_string in filter_info["匹配规则"]:
                    self._header_list.append(key_string)
            if filter_info["匹配类型"] == "正则匹配":
                pass  # 暂时不处理

        return self._header_list


class ModelLogFilterCfg(object):
    def __init__(self):
        pass
